package com.ls.fw.mvc.struts.base;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ls.fw.commons.core.utils.date.DateFormatUtil;
import com.ls.fw.commons.core.utils.date.DateUtils;
import com.ls.fw.mvc.struts.base.result.ActionResult;
import com.ls.fw.mvc.struts.constant.Constants;
import com.ls.fw.mvc.struts.constant.OperateType;
import com.ls.fw.mvc.struts.constant.ResultType;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.PreResultListener;
/**
 * action抽象基类
 * @author Defender
 * 2013-7-1
 */
public  abstract class BaseAction extends ActionSupport implements ServletContextAware,
		ServletResponseAware, ServletRequestAware, SessionAware, RequestAware,ApplicationAware  {

	private static final long serialVersionUID = 1L;
	//header 常量定义
	public static final String ENCODING_PREFIX = "encoding";
	public static final String NOCACHE_PREFIX = "no-cache";
	public static final String ENCODING_DEFAULT = "UTF-8";
	public static final boolean NOCACHE_DEFAULT = true;
	public static final String STATUS_NAME = "status";
	public static final String MSG_NAME = "msg";
	
	public static final int MAX_AGE_UNIT = 3600;//h
	
	public static final int MAX_AGE = 24 * MAX_AGE_UNIT;//24h
	

	protected final Logger logger = LoggerFactory.getLogger(this.getClass());

	protected ServletContext application = null;
	protected HttpServletRequest request = null;
	protected HttpServletResponse response = null;
	protected HttpSession session = null;

	protected Map<?, ?> sessionMap = null;
	protected Map<?, ?> requestMap = null;
	protected Map<?, ?> applicationMap = null;
	
	/**
	 * ajax操作返回值
	 */
	protected int status = ResultType.FAIL.getValue();
	protected String msg = "";
	
	/**
	 * 数据库操作类型：用于页面判断（如：是查看则不可编辑等）
	 */
	protected String crud = OperateType.SELECT;
	

	/**
	 * json数据
	 */
	private Map<String,Object> jsonData = new HashMap<String,Object>(1);

	/**
	 * 只能在子类中 实例化
	 */
	protected BaseAction(){
		super();
	}
	

	public Map<String, Object> getJsonData() {
		return jsonData;
	}

	public int getStatus() {
		return status;
	}


	public String getMsg() {
		return msg;
	}

	/**
	 * 添加需要返回的json数据
	 * @author Defender
	 * 2014年3月1日 下午9:55:10
	 * @param name
	 * @param value
	 * @return
	 */
	public Object putJsonData(String name,Object value){
		return this.jsonData.put(name, value);
	}

	/**
	 * 是否ajax请求
	 * @author Defender
	 * 2013-8-7 下午09:16:11
	 * @param request
	 * @return
	 */
	protected boolean isAjaxRequest(HttpServletRequest request) {
		String header = request.getHeader("X-Requested-With");
		if (header != null && "XMLHttpRequest".equalsIgnoreCase(header))
			return true;
		else
			return false;
	}
	
	/**
	 * 获取action上下文对象，用于获取访问servlet的API(方式2)
	 * @author Defender
	 * 2013-7-7 下午03:09:26
	 * @return
	 */
	protected ActionContext getContext(){
		return ActionContext.getContext();
	}
	
	/**
	 * 获取Request,用于获取访问servlet的API(方式3)
	 * @author Defender
	 * 2013-7-7 下午03:10:09
	 * @return
	 */
	protected HttpServletRequest getServletRequest(){
		return ServletActionContext.getRequest();
	}
	
	/**
	 * 获取Responsek.,用于获取访问servlet的API(方式3)
	 * @author Defender
	 * 2013-7-7 下午03:12:20
	 * @return
	 */
	protected HttpServletResponse getServletResponse(){
		return ServletActionContext.getResponse();
	}
	
	/**
	 * 注册返回结果前的监听器
	 * @author Defender
	 * 2013-7-7 下午03:31:11
	 */
	protected void addPreResultListener(PreResultListener pr){
		ActionContext context = ActionContext.getContext();
		ActionInvocation ai = context.getActionInvocation();
		ai.addPreResultListener(pr);
	}
	 
	/**
	 * 某实体是否存在
	 * @return
	 */
	public boolean isExist(){
		logger.debug("isExist");
		return true;
	}
	
	
	public String index()throws Exception{
		logger.debug("index");
		return ActionResult.SUCCESS;
	}
	
	/**
	 * 添加action的初始化
	 * @return
	 * @throws Exception
	 */
	public  String init_add()throws Exception{
		logger.debug("init_add");
		return ActionResult.SUCCESS;
	}
	
	/**
	 * 保存：添加或更新
	 * @author Defender
	 * 2014年3月8日 下午11:19:55
	 * @return
	 * @throws Exception
	 */
	public  String save()throws Exception{
		logger.debug("save");
		return ActionResult.SUCCESS;
	}
	
	
	/**
	 * 初始化编辑：添加或更新
	 * @author Defender
	 * 2014年3月8日 下午11:19:29
	 * @return
	 * @throws Exception
	 */
	public  String init_edit()throws Exception{
		logger.debug("init_edit");
		return ActionResult.SUCCESS;
	}
	
	/**
	 * 更新action的初始化
	 * @return
	 * @throws Exception
	 */
    public  String init_update()throws Exception{
    	logger.debug("init_update");
		return ActionResult.SUCCESS;
	}
	
    /**
     * 分页查询action的初始化
     * @return
     * @throws Exception
     */
    public  String init_query()throws Exception{
    	logger.debug("init_query");
	    return ActionResult.SUCCESS;
    }

    /**
     * 添加内容
     * @return
     * @throws Exception
     */
    public  String add()throws Exception{
    	logger.debug("add");
	    return ActionResult.SUCCESS;
    }

    /**
     * 更新内容
     * @return
     * @throws Exception
     */
    public  String update()throws Exception{
    	logger.debug("update");
	    return ActionResult.SUCCESS;
    }

    /**
     * 物理删除
     * @return
     * @throws Exception
     */
    public  String delete()throws Exception{
    	logger.debug("delete");
	    return ActionResult.SUCCESS;
    }

    /**
     * 逻辑删除
     * @return
     * @throws Exception
     */
    public  String remove()throws Exception{
    	logger.debug("remove");
	    return ActionResult.SUCCESS;
    }

    /**
     * 查询单个实体
     * @return
     * @throws Exception
     */
    public  String find()throws Exception{
    	logger.debug("find");
	    return ActionResult.SUCCESS;
    }
   
    /**
     * 查看实体详情
     * @return
     * @throws Exception
     */
    public  String view()throws Exception{
    	logger.debug("view");
	    return ActionResult.SUCCESS;
    }

    /**
     * 查询list数据 
     * @author Defender
     * 2013-7-1 下午09:37:50
     * @return
     * @throws Exception
     */
    public String list()throws Exception{
    	logger.debug("list");
	    return ActionResult.SUCCESS;
    }
    
    /**
     * 分页查询
     * @return
     * @throws Exception
     */
    public  String query()throws Exception{
    	logger.debug("query");
	    return ActionResult.SUCCESS;
    }
 
    /**
     * 获取请求消息头
     * @author Defender
     * 2013-7-7 上午10:14:49
     * @return
     */
    @SuppressWarnings("unchecked")
	protected String getRequestHead(){
    	StringBuilder head = new StringBuilder();
        Enumeration<String>  headNames = this.request.getHeaderNames();
        if(headNames!=null){
        	while(headNames.hasMoreElements()){
        		String headName = headNames.nextElement();
        		head.append(headName+"-->"+this.request.getHeader(headName)+"\n");
        	}
        }
    	return head.toString();
    }
    
    
    /**
     * 获取请求参数
     * @author Defender
     * 2013-7-7 上午10:20:44
     * @return
     */
    protected String getRequestPara(){
    	return this.request.getQueryString();
    }
    
    
    /**
     * 解码参数
     * @author Defender
     * 2013-7-7 上午10:20:38
     * @param para
     * @param enc
     * @return
     * @throws UnsupportedEncodingException
     */
    protected String decoderPara(final String para,final String enc) throws UnsupportedEncodingException{
    	return URLDecoder.decode(para, enc);
    }
    
    /**
     * 解码参数
     * @author Defender
     * 2013-7-7 下午01:01:26
     * @param para
     * @return
     * @throws UnsupportedEncodingException
     */
    protected String decoderPara(final String para) throws UnsupportedEncodingException{
    	return decoderPara(para, ENCODING_DEFAULT);
    }
    
    
    /**
     * 编码字符
     * @author Defender
     * 2013-7-7 下午12:59:48
     * @param para
     * @param enc
     * @return
     * @throws UnsupportedEncodingException
     */
    protected String encoderPara(final String para,final String enc) throws UnsupportedEncodingException{
    	return URLEncoder.encode(para, enc);
    }
    
    /**
     * 编码字符
     * @author Defender
     * 2013-7-7 下午01:00:53
     * @param para
     * @return
     * @throws UnsupportedEncodingException
     */
    protected String encoderPara(final String para) throws UnsupportedEncodingException{
    	return encoderPara(para, ENCODING_DEFAULT);
    }
    
    /**
     * 发送ajax操作结果
     * @author Defender
     * 2013-7-1 下午09:17:25
     * @return void
     */
	protected void sendAJAXTip() {  
		Map<String,Object> map = new HashMap<String,Object>(2);
		map.put(STATUS_NAME, this.status);
		map.put(MSG_NAME, this.msg);
		this.sendJSONObject(map);
	}
	
    /**
     * 发送多个键值对json数据到页面
     * @author Defender
     * 2013-7-1 下午09:13:37
     * @param map
     * @return void
     */
    protected void sendJSONObject(final Map<String,Object> map) {  
        JSONObject jsonObj = new JSONObject();
        jsonObj.putAll(map);
        this.sendJsonData(jsonObj);
    } 
    
    
	/**
	 * 发送一个键值对json数据到页面
	 * @author Defender
	 * 2013-7-1 下午09:13:29
	 * @param objName
	 * @param obj
	 * @return void
	 */
    protected void sendJSONObject(final String  objName, final Object obj) {
        JSONObject jsonObj = new JSONObject();
        jsonObj.put(objName, obj); 
        this.sendJsonData(jsonObj);
    }  
    


   /**
    * 发送json数据到页面
    * @author Defender
    * 2013-7-1 下午09:12:17
    * @param jsonObj
    * @return void
    */
    protected void sendJsonData(final JSONObject jsonObj){
    	logger.debug("sendJsonData");
    	String encoding = "UTF-8";  
        String contentType = "application/json";  
        contentType = contentType + ";charset=" + encoding;  
        HttpServletResponse response = this.response;
        response.setContentType(contentType);  
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
        PrintWriter pw = null;
		try {
			pw = response.getWriter();
			pw.write(jsonObj.toString());  
		    pw.flush();   
		} catch (IOException e) {
			logger.debug("sendJsonData:"+e.getMessage());
		}   
    }
  
	
	
	/**
	 * 设置页面不缓存
	 * @author Defender
	 * 2013-7-1 下午09:33:23
	 * @return void
	 */
	protected void setResponseNoCache() {
		response.setHeader("progma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Cache-Control", "no-store");
		response.setDateHeader("Expires", 0);
	}
	
	/**
	 *  向页面打印信息
	 * @author Defender
	 * 2013-7-28 下午03:26:21
	 * @param obj
	 */
	public void write(Object obj) {
		try {
			response.setContentType("text/html;charset=utf-8");
			response.getWriter().print(obj);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 直接输出文本.
	 * @see #render(String, String, String...)
	 */
	public  void renderText(final String text, final String... headers) {
		render("text/plain", text, headers);
	}

	/**
	 * 直接输出HTML.
	 * @see #render(String, String, String...)
	 */
	public  void renderHtml(final String html, final String... headers) {
		render("text/html", html, headers);
	}

	/**
	 * 直接输出XML.
	 * @see #render(String, String, String...)
	 */
	public  void renderXml(final String xml, final String... headers) {
		render("text/xml", xml, headers);
	}

	/**
	 * 直接输出JSON.
	 * 
	 * @param string json字符串.
	 * @see #render(String, String, String...)
	 */
	public  void renderJson(final String string, final String... headers) {
		render("application/json", string, headers);
	}

	/**
	 * 保存为cookie
	 * @author Defender
	 * 2013-7-7 下午12:47:37
	 * @param name
	 * @param value
	 * @throws UnsupportedEncodingException 
	 */
	protected void saveCookie(final String name,final Object value) throws UnsupportedEncodingException{
		this.saveCookie(name, value.toString(),MAX_AGE);
	}

	/**
	 * 保存为cookie
	 * @author Defender
	 * 2013-7-7 下午12:47:55
	 * @param name
	 * @param value
	 * @param maxAge 保存时间，单位为h
	 * @throws UnsupportedEncodingException 
	 */
	protected void saveCookie(final String name,final  Object value,final int maxAge) throws UnsupportedEncodingException{
		Cookie cookie = new Cookie(name, this.encoderPara(value.toString()));
		cookie.setMaxAge(maxAge);
		this.response.addCookie(cookie);
	}
	
	/**
	 * 删除cookie
	 * @author Defender
	 * 2013-7-7 下午12:54:28
	 * @param name
	 * @return
	 */
	protected boolean removeCookie(final String name){
		boolean flag = false;
		Cookie[] cookie = this.request.getCookies();
		if(cookie!=null){
			for (Cookie cookie2 : cookie) {
				if(name.trim().equals(cookie2.getName())){
					cookie2.setMaxAge(0);
					flag = true;
					break;
				}
			}
		}
		return flag;
	}
	
	
	/**
	 * 获取cookie中的值
	 * @author Defender
	 * 2013-7-7 下午12:56:39
	 * @param name
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	protected String getCookieValue(final String name) throws UnsupportedEncodingException{
		String value = null;
		Cookie[] cookie = this.request.getCookies();
		if(cookie!=null){
			for (Cookie cookie2 : cookie) {
				if(name.trim().equals(cookie2.getName())){
					value = this.decoderPara(cookie2.getValue());
					break;
				}
			}
		}
		return value;
	}
	
	public void resetExportExcelProp(String fileName){
		this.response.reset();
//		try {
//			fileName = URLEncoder.encode(new String(fileName.getBytes("UTF-8"), "UTF-8"), "UTF-8");
//		} catch (UnsupportedEncodingException e) {
//			logger.error("导出文件名编码转换错误", e);
//		}
//		try {
//			byte[] byname = fileName.getBytes("gbk");
//			fileName = new String(byname,"8859_1");
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
		
		this.response.setContentType("application/vnd.ms-excel;charset=UTF-8");
		this.response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + ".xls\"");
		this.response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
		this.response.setHeader("Pragma", "public");
		this.response.setDateHeader("Expires", (System.currentTimeMillis() + 1000));
    }
	
	/**
	 * 直接输出内容的简便函数.

	 * eg.
	 * render("text/plain", "hello", "encoding:GBK");
	 * render("text/plain", "hello", "no-cache:false");
	 * render("text/plain", "hello", "encoding:GBK", "no-cache:false");
	 * 
	 * @param headers 可变的header数组，目前接受的值为"encoding:"或"no-cache:",默认值分别为UTF-8和true.
	 */
	public  void render(final String contentType, final String content, final String... headers) {
		try {
			//分析headers参数
			String encoding = ENCODING_DEFAULT;
			boolean noCache = NOCACHE_DEFAULT;
			for (String header : headers) {
				String headerName = StringUtils.substringBefore(header, ":");
				String headerValue = StringUtils.substringAfter(header, ":");

				if (StringUtils.equalsIgnoreCase(headerName, ENCODING_PREFIX)) {
					encoding = headerValue;
				} else if (StringUtils.equalsIgnoreCase(headerName, NOCACHE_PREFIX)) {
					noCache = Boolean.parseBoolean(headerValue);
				} else
					throw new IllegalArgumentException(headerName + "不是一个合法的header类型");
			}

			HttpServletResponse response = this.response;

			//设置headers参数
			String fullContentType = contentType + ";charset=" + encoding;
			response.setContentType(fullContentType);
			if (noCache) {
				response.setHeader("Pragma", "No-cache");
				response.setHeader("Cache-Control", "no-cache");
				response.setDateHeader("Expires", 0);
			}
			response.getWriter().write(content);
			response.getWriter().flush();
		} catch (IOException e) {
			logger.error(e.getMessage(), e);
		}
	}
	
	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		this.request = arg0;  
		this.session = request.getSession();
	}
	
	
	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		this.response = arg0; 
	}

    
	@Override 
	public void setServletContext(ServletContext application) {
		this.application = application;
	}
	

	@Override
	public void setSession(Map<String, Object> arg0) {
		this.sessionMap = arg0;
		
	}
	@Override
	public void setRequest(Map<String, Object> request) {
		this.requestMap = request;
	} 
	@Override
	public void setApplication(Map<String, Object> application) {
		this.applicationMap = application;
	}
	
	/**
	 * 添加需要返回到页面的数据
	 * @param key 键
	 * @param value 值
	 */
	public void put(String key, Object value) {
		if(this.isAjaxRequest(this.request)){
			this.putJsonData(key, value);
		}else{
			this.getContext().put(key, value);
		}
	}
	
	
	/**
	 * 返回状态
	 * @author 李昇
	 * 2014年4月30日 下午10:11:10
	 * @param value
	 */
	public void putStatus(ResultType status) {
		this.status = status.getValue();
		this.put(STATUS_NAME, status.getValue());
	}
	
	
	/**
	 * 返回成功信息
	 * @author 李昇
	 * 2014年4月30日 下午10:11:01
	 * @param msg
	 */
	public void putSuccess(Object msg) {
		this.status = ResultType.SUCCESS.getValue();
		this.put(STATUS_NAME, status);
		this.msg = (String) msg;
		this.put(MSG_NAME, msg);
	}
	
	
	
	/**
	 * 返回失败信息
	 * @author 李昇
	 * 2014年4月30日 下午10:10:33
	 * @param msg
	 */
	public void putFail(Object msg) {
		this.status = ResultType.FAIL.getValue();
		this.put(STATUS_NAME, status);
		this.msg = (String) msg;
		this.put(MSG_NAME, msg);
	}
	
	/**
	 * 返回信息
	 * @author 李昇
	 * 2014年4月30日 下午10:10:47
	 * @param value
	 */
	public void putMsg(Object value) {
		this.msg = (String) value;
		this.put(MSG_NAME, value);
	}

	/**
	 * 获取提交值
	 * @param name
	 * @return
	 */
	public String getParameter(String name) {
		return this.request.getParameter(name);
	}
	
	/**
	 * 获取提交的某个值
	 * @param name
	 * @return
	 */
	public Date getDate(String name) {
		try {
			String value = this.getParameter(name);
			if (value != null && !"".equals(value.trim())) {
				return DateUtils.parseDate(value);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return null;
	}
	
	/**
	 * String 转 double
	 * @author ls 2013-10-24 上午11:17:55
	 * @param name
	 * @param defaultvalue
	 * @return
	 */
	public double getDouble(String name, double defaultvalue) {
		String value = getParameter(name);
		try {
			if (value != null && !"".equals(value.trim())) {
				return Double.parseDouble(value);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return defaultvalue;
	}
	
	/**
	 * String 转 double
	 * @author ls 2013-10-24 上午11:18:17
	 * @param name
	 * @return
	 */
	public double getDouble(String name) {
		try {
			String value = this.request.getParameter(name);
			return Double.parseDouble(value);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return 0;
	}
	
	/**
	 * String 转 long
	 * @author ls 2013-10-24 上午11:18:28
	 * @param name
	 * @param defaultvalue
	 * @return
	 */
	public long getLong(String name, long defaultvalue) {
		try {
			String value = getParameter(name);
			if (value == null)
				return defaultvalue;
			return Long.parseLong(value);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return defaultvalue;
	}
	
	/**
	 * String 转 long
	 * @author ls 2013-10-24 上午11:18:28
	 * @param name
	 * @return
	 */
	public long getLong(String name) throws Exception {
		try {
			String value = this.request.getParameter(name);
			return Long.parseLong(value);
		} catch (Exception ex) {
			throw new Exception(ex.getMessage());
		}
	}
	
	/**
	 * String 转 float
	 * @author ls 2013-10-24 上午11:18:28
	 * @param name
	 * @param defaultvalue
	 * @return
	 */
	public float getFloat(String name, float defaultvalue) {
		try {
			String value = this.request.getParameter(name);
			if (value != null && !"".equals(value)) {
				return Float.parseFloat(value);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return defaultvalue;
	}
	
	/**
	 * String 转 float
	 * @author ls 2013-10-24 上午11:19:15
	 * @param name
	 * @return
	 * @throws Exception
	 */
	public float getFloat(String name) throws Exception {
		try {
			String value = this.request.getParameter(name);
			return Float.parseFloat(value);
		} catch (Exception ex) {
			throw new Exception(ex.getMessage());
		}
	}
	
	/**
	 * 从request得到参数
	 * @author ls 2013-10-24 上午11:19:34
	 * @param name
	 * @param defaultvalue
	 * @return
	 */
	public String getString(String name, String defaultvalue) {
		String value = this.request.getParameter(name);
		if (value != null)
			return value;
		return defaultvalue;
	}
	
	/**
	 * 从request得到参数
	 * @author ls 2013-10-24 上午11:20:09
	 * @param name
	 * @return
	 * @throws Exception
	 */
	public String getString(String name) throws Exception {
		String value = this.request.getParameter(name);
		if (value != null)
			return value;
		throw new Exception("参数不存在");
	}
	/**
	 * 从request得到参数
	 * @author ls 2013-10-24 上午11:20:09
	 * @param name
	 * @return
	 * @throws Exception
	 */
	public String[] getStringArray(String name) throws Exception {
		String[] value = this.request.getParameterValues(name);
		if (value != null)
			return value;
		throw new Exception("参数不存在");
	}
	/**
	 * 从request得到int参数
	 * @author ls 2013-10-24 上午11:20:24
	 * @param name
	 * @param defaultvalue
	 * @return
	 */
	public int getInt(String name, int defaultvalue) {
		try {
			
			String value = getParameter(name);
			if (value != null && !"".equals(value.trim())) {
				return Integer.parseInt(value);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return defaultvalue;
	}
	
	/**
	 * 从request昨到int参数
	 * @author ls 2013-10-24 上午11:20:55
	 * @param name
	 * @return
	 * @throws Exception
	 */
	public int getInt(String name) throws Exception {
		try {
			String value = this.request.getParameter(name);
			return Integer.parseInt(value);
		} catch (Exception ex) {
			throw new Exception(ex.getMessage());
		}
	}
	
	/*
	 * public java.sql.Date getDate(String name) throws Exception { try { String value = this.request.getParameter(name); return
	 * java.sql.Date.valueOf(value); } catch (Exception ex) { throw new Exception(ex.getMessage()); } }
	 */

	/**
	 * 参数转为java.sql.Date
	 */
	public java.sql.Date getDate(String name, java.sql.Date defaultvalue) {
		try {
			String value = this.request.getParameter(name);
			return java.sql.Date.valueOf(value);
		} catch (Exception ex) {
		}
		return defaultvalue;
	}
	
	/**
	 * 参数转为java.sql.Timestamp
	 * @author ls 2013-10-24 上午11:21:36
	 * @param name
	 * @param defaultvalue
	 * @return
	 */
	public java.sql.Timestamp getTimestamp(String name, java.sql.Timestamp defaultvalue) {
		try {
			String value = this.request.getParameter(name);
			return java.sql.Timestamp.valueOf(value);
		} catch (Exception ex) {
		}
		return defaultvalue;
	}
	
	// 添加 YC
	public java.sql.Timestamp getTimestamp(String name) throws Exception {
		try {
			String value = this.request.getParameter(name);
			return java.sql.Timestamp.valueOf(value);
		} catch (Exception ex) {
			throw new Exception(ex.getMessage());
		}
	}
	

	public String getCrud() {
		return crud;
	}


	public void setCrud(String crud) {
		this.crud = crud;
	}
	/**
	 * 获取用户session
	 * @author Defender
	 * 2014年3月1日 下午10:29:20
	 * @return
	 */
	public Object getSessionUser(){
		 return this.session.getAttribute(Constants.CURRENT_USER);
	}
	
	/**
	 * 设置用户session
	 * @author Defender
	 * 2014年3月1日 下午10:29:04
	 * @param obj
	 */
	public void setSessionUser(Object obj){
		   this.session.setAttribute(Constants.CURRENT_USER,obj);
	}
	
	public void clearSessionUser(){
		   this.session.removeAttribute(Constants.CURRENT_USER);
	}
	
	
	//,Preparable
	//@Override
	public void prepare() throws Exception {
		logger.debug("prepare....");
		this.put("crud", this.crud);
	} 
}
